package com.neusmart.tongrentang.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.neusmart.tongrentang.F;
import com.neusmart.tongrentang.R;
import com.neusmart.tongrentang.model.Emoji;

import java.util.List;

/**
 * Emoji Adapter
 * <p/>
 * keven
 */
public class EmojiAdapter extends ArrayAdapter<Emoji> {

    private Context context;

    public EmojiAdapter(Context context, List<Emoji> objects) {
        super(context, 0, objects);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_emoji, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        int width = (F.mDisplayWidth - 100) / 7;
        convertView.setLayoutParams(new AbsListView.LayoutParams(width, width));
        Emoji item = getItem(position);
        holder.setContent(item);
        return convertView;
    }

    private final class ViewHolder {

        ImageView ivEmoji;

        public ViewHolder(View v) {
            ivEmoji = (ImageView) v.findViewById(R.id.item_iv_emoji);
        }

        public void setContent(Emoji item) {
            ivEmoji.setImageResource(item.getImageResId(context));
        }

    }
}
